﻿///***************************************************************
/// 시  스  템 : Base
/// 단위시스템 : Dac
/// 계      층 : BaseProject Layer
/// 프로그램명 : AccessCheck
/// 파  일  명 : AccessCheck.cs
/// 설      명 : 엑세스 체크 클래스
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-07-18 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Data.SqlClient;
using System.EnterpriseServices;
using System.Runtime.InteropServices;
using System.Web.Security;

using SMFramework.Data;
using SMFramework.Configuration;



namespace Base.Dac
{
	[ComVisible(true)]
	[Transaction(TransactionOption.Supported)]
	[JustInTimeActivation(true)]
	public class AccessCheck : Base.AppFramework.DacBase
	{
		//SetVisibleCtrl
		AppSettingsReader arVisibleCtrl = new AppSettingsReader("Base.AppFramework/VisibleCtrl");
		
		public AccessCheck()
		{
		}

		#region CheckLoginInfo

		/// <summary>
		/// Id, Pw로 로그인 정보를 조회한다.
		/// </summary>
		/// <param name="lo_dp"></param>
		/// <returns></returns>
		[AutoComplete]
		public DataSet CheckLoginInfo(DataPack lo_dp)
		{
			string ls_sql = "usp_UPDMS_COM_USER_MGT_Get_Login_Info";
			SqlParameter[] paramArray = lo_dp.ToSqlParameters();

			DataSet lo_ds = new DataSet();
			return _agent.Fill(ls_sql, "UPDMS_COM_USER_MGT", lo_ds, paramArray, CommandType.StoredProcedure);
		}
		
		#endregion

		#region IsRegisteredUser

		/// <summary>
		/// Id, Pw로 등록된 사용자인지 체크
		/// </summary>
		/// <param name="lo_dp"></param>
		/// <returns></returns>
		[AutoComplete]
		public bool IsRegisteredUser(DataPack lo_dp)
		{
			bool lb_result = false;
			string ls_sql = "usp_UPDMS_COM_USER_MGT_Get_Login_Info";

			SqlParameter[] paramArray = lo_dp.ToSqlParameters();

			int li_result = (int)_agent.ExecuteScalar(ls_sql, paramArray, CommandType.StoredProcedure);

			//결과값이 있으면 true
			lb_result = (li_result == 1) ? true : false;

			return lb_result;
			
		}

		#endregion

		#region GetStartPage

		/// <summary>
		/// Id로 시작페이지를 조회한다.
		/// </summary>
		/// <param name="ls_user_id"></param>
		/// <returns></returns>
		[AutoComplete]
		public string GetStartPage(string ls_user_id)
		{
			string ls_sql = "usp_UPDMS_COM_USER_MGT_Get_Start_Page";
			
			SqlParameter[] paramArray =
			{
				new SqlParameter("ls_user_id", ls_user_id)
			};
			
			return (string)_agent.ExecuteScalar(ls_sql, paramArray, CommandType.StoredProcedure);
		}

		#endregion

		#region CheckPgmAccessGrant

		/// <summary>
		/// 로그인한 사용자의 User_Level이 접근한 프로그램의 Access_Level이상인지 여부
		/// </summary>
		/// <param name="ls_pgm_id"></param>
		/// <param name="ls_user_id"></param>
		/// <returns></returns>
		[AutoComplete]
		public bool CheckPgmAccessGrant(string ls_pgm_id, string ls_user_id, DataPack dp)
		{
			try
			{
				//로그기록
				dp.AddProperty("Pgm_Id", typeof(string), ls_pgm_id);
				CreateAccessLog(dp);
			}
			catch(Exception ex)
			{
				AppFramework.SMEventLog.WriteExceptionLog(ex);
			}

			bool lb_result = false;
			string ls_sql = "usp_UPDMS_COM_PGM_Get_Pgm_Access_Grant";

			SqlParameter[] paramArray =
			{
				new SqlParameter("ls_pgm_id", ls_pgm_id),
				new SqlParameter("ls_user_id", ls_user_id)
			};

			string ls_temp = (string)_agent.ExecuteScalar(ls_sql, paramArray, CommandType.StoredProcedure);

			lb_result = (ls_temp == "T") ? true : false;

			return lb_result;
		}

		#endregion

		#region CheckPgmUsingGrant

		/// <summary>
		/// 로그인한 사용자의 User_Div가 접근한 프로그램의 사용권한에 등록되어있는지 여부
		/// </summary>
		/// <param name="ls_user_div"></param>
		/// <param name="ls_pgm_id"></param>
		/// <returns></returns>
		[AutoComplete]
		public bool CheckPgmUsingGrant(string ls_user_div, string ls_pgm_id)
		{
			bool lb_result = false;
			string ls_sql = "usp_UPDMS_COM_PGM_GRANT_Get_Pgm_Using_Grant";

			SqlParameter[] paramArray =
			{
				new SqlParameter("ls_user_div", ls_user_div),
				new SqlParameter("ls_pgm_id", ls_pgm_id)
			};
			
			string ls_temp = (string)_agent.ExecuteScalar(ls_sql, paramArray, CommandType.StoredProcedure);
			
			lb_result = (ls_temp == "T") ? true : false;
				
			return lb_result;
		}
		
		#endregion

		#region IsAdministrator

		/// <summary>
		/// ID, 비밀번호로 Administrator인지 검사
		/// </summary>
		/// <param name="ls_user_id"></param>
		/// <param name="ls_pw"></param>
		/// <returns></returns>
		[AutoComplete]
		public bool IsAdministrator(string ls_user_id, string ls_pw)
		{
			bool lb_result = false;
			string ls_sql = "usp_UPDMS_COM_USER_MGT_Get_Administrator";
			
			SqlParameter[] paramArray =
			{
				new SqlParameter("ls_user_id", ls_user_id),
				new SqlParameter("ls_pw", FormsAuthentication.HashPasswordForStoringInConfigFile(ls_pw, "SHA1"))	// 암호화
			};			

			int li_result = (int)_agent.ExecuteScalar(ls_sql, paramArray, CommandType.StoredProcedure);
			
			//결과값이 있으면 true
			lb_result = (li_result == 1) ? true : false;

			return lb_result;
		}

		#endregion

		#region GetVisibleCtrl
		/// <summary>
		/// 프로그램아이디로  web.config의 VisibleCtrl에 있는 콘트롤목록을 가져온다
		/// </summary>
		/// <param name="ls_pid"></param>
		/// <returns></returns>
		public string GetVisibleCtrl(string ls_pid)
		{
			string ls_rslt = "";

			try
			{
				ls_rslt = arVisibleCtrl[ls_pid];
			}
			catch (Exception)
			{
			}
			
			return ls_rslt;
		}

		#endregion

		#region CreateAccessLog

		/// <summary>
		/// 접속로그 기록
		/// </summary>
		/// <param name="lo_dp"></param>
		/// <returns></returns>
		[AutoComplete]
		public int CreateAccessLog(DataPack lo_dp)
		{
			string strSql = "usp_UPDMS_COM_PGM_ACCESS_LOG_Insert";

			SqlParameter[] paramArray = lo_dp.ToSqlParameters();

			return _agent.ExecuteNonQuery(strSql, paramArray, CommandType.StoredProcedure);
		}

		#endregion

		#region IsPwExpiredUsingYn

		/// <summary>
		/// 비밀번호 만료기능 사용여부 리턴
		/// </summary>
		/// <param name="lo_dp"></param>
		/// <returns></returns>
		[AutoComplete]
		public bool IsPwExpiredUsingYn(DataPack lo_dp)
		{
			string ls_result = "N";
			bool lb_result = false;
			string ls_sql = "usp_UPDMS_COM_USER_MGT_Get_Pw_Expired_Using_Yn";

			SqlParameter[] paramArray = lo_dp.ToSqlParameters();

			ls_result = (string)_agent.ExecuteScalar(ls_sql, paramArray, CommandType.StoredProcedure);
			lb_result = (ls_result == "Y");
			return lb_result;
		}

		#endregion
		
	}
}
